<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kabupaten extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		if((!$this->session->userdata('user_display')) && (!$this->session->userdata('id_user'))):
			redirect('login');
		endif;
		$this->load->model('Common_model');
		$this->load->helper('bantuan_helper');
	}

	function index()
	{
		$offset = $this->uri->segment(3);
		$config['base_url'] = site_url().'/kabupaten/index';
		$config['total_rows'] = count($this->Common_model->getData('list','tb_agama',FALSE,FALSE,FALSE,FALSE));
		$config['per_page'] = $this->config->item('per_page');
		$config['uri_segment'] = '3';
		$data['urut'] = $this->uri->segment(3);
		$data['slug'] = anchor('kabupaten','Data Kabupaten/Kota');
		$data['title'] = "Data Kabupaten/Kota";
		$data['query'] = $this->Common_model->getData('list','tb_agama',FALSE,FALSE,$config['per_page'],$offset);
		$data['main_view'] = "kabupaten/index";
		$this->pagination->initialize($config);
		$this->load->view('index',$data);
	}
	
	function view()
	{
		if ($this->uri->segment(3) !=''):
			$id_prov = $this->uri->segment(3);
		else:
			$id_prov = $this->input->post('id_provinsi');
		endif;
		
		$this->db->where('id_provinsi',$id_prov);
		$res = $this->db->get('tb_kabupaten')->result_array();
		
		$this->db->where('id_provinsi',$id_prov);
		$row = $this->db->get('tb_provinsi')->row();
		
		$data['slug'] = anchor('kabupaten','Data Kabupaten/Kota');

		$data['id_prov'] = $id_prov;
		$data['urut'] = $this->uri->segment(3);
		$data['title'] = "Data Kabupaten/Kota";
		$data['query'] = $res;
		$data['main_view'] = "kabupaten/view_kab";

		$this->load->view('index',$data);
	}
	
	function add()
	{
		$this->form_validation->set_rules('id_provinsi', 'Provinsi','required');
		$this->form_validation->set_rules('nama_kab', 'Nama Kabupaten/Kota','required');
		
		if($this->form_validation->run() == FALSE)
		{
			if ($this->uri->segment(3) !=''):
				$id_prov = $this->uri->segment(3);
				$this->db->where('id_provinsi',$id_prov);
				$data['row'] = $this->db->get('tb_provinsi')->row();
				$data['id_prov'] = $id_prov;
				$data['slug'] = anchor('kabupaten','Data Kabupaten/Kota').' &raquo; '.anchor('kabupaten/view/'.$id_prov,'Provinsi '.$data['row']->nama_provinsi).' &raquo; '.'Tambah Data Kabupaten/Kota';
			else:
				$id_prov = $this->uri->segment(3);
				$data['id_prov'] = $id_prov;
				$data['slug'] = anchor('kabupaten','Data Kabupaten/Kota').' &raquo; '.'Tambah Data Kabupaten/Kota';
			endif;
			$data['title'] = 'Tambah Data Kabupaten/Kota';
			$data['main_view'] = "kabupaten/add";
			$this->load->view('index',$data);
		}
		else
		{
			if ($this->uri->segment(3) !=''):
				$id_prov = $this->uri->segment(3);
			else:
				$id_prov = $this->input->post('id_provinsi');
			endif;
			$data = array(
					'id_provinsi'=>$this->input->post('id_provinsi'),
					'nama_kab'=>$this->input->post('nama_kab')
					);
			$this->Common_model->addData('tb_kabupaten',$data);
			redirect('kabupaten/view/'.$id_prov);
		}
	
	}
	
	function edit()
	{
		$this->form_validation->set_rules('id_provinsi', 'Provinsi','required');
		$this->form_validation->set_rules('nama_kab', 'Nama Kabupaten/Kota','required');
		
		if($this->form_validation->run() == FALSE)
		{
	
			$id_prov = $this->uri->segment(4);
			$this->db->where('id_provinsi',$id_prov);
			$data['row'] = $this->db->get('tb_provinsi')->row();
			$data['id_prov'] = $id_prov;
			
			$id_kab = $this->uri->segment(3);
			$this->db->where('id_kab',$id_kab);
			$data['row2'] = $this->db->get('tb_kabupaten')->row();
			
			$data['slug'] = anchor('kabupaten','Data Kabupaten/Kota').' &raquo; '.anchor('kabupaten/view/'.$id_prov,'Provinsi '.$data['row']->nama_provinsi).' &raquo; Edit Data Kabupaten/Kota '.$data['row2']->nama_kab;

			$data['title'] = 'Edit Data Kabupaten/Kota';
			$data['main_view'] = "kabupaten/edit";
			$this->load->view('index',$data);
		}
		else
		{
			if ($this->uri->segment(3) !=''):
				$id_prov = $this->uri->segment(3);
			else:
				$id_prov = $this->input->post('id_provinsi');
			endif;
			$data = array(
					'id_provinsi'=>$this->input->post('id_provinsi'),
					'nama_kab'=>$this->input->post('nama_kab')
					);
			$this->Common_model->addData('tb_kabupaten',$data);
			redirect('kabupaten/view/'.$id_prov);
		}
	
	}
	
	function delete()
	{
		$id = $this->uri->segment(3);
		$id_prov = $this->uri->segment(4);
		$this->Common_model->deleteData('tb_kabupaten','id_kab',$id);
		redirect('kabupaten/view/'.$id_prov);
	}
	
	function ajax_showKab()
	{
		$provId=$this->uri->segment(3);
		$this->db->where('id_provinsi',$provId);
		$query = $this->db->get('tb_kabupaten')->result_array();
		
		echo '<p id="bottom_link"><a class="add" href="'.site_url().'kabupaten/add/'.$provId.'">Tambah Kabupaten/Kota</a></p>';
		echo '<table border="0" cellpadding="0" cellspacing="0">';
			echo '<tr>';
				echo '<th><div align="center">No</div></th>';
				echo '<th><div align="center">Nama Kabupaten</div></th>';
				echo '<th><div align="center">Aksi</div></th>';
				echo '</tr>';

			$i = 1 ;
			foreach($query as $row):  
				if ($i %2 == 0):
					echo '<tr class="zebra">'; 
				else: 
					echo '<tr class="zebra2">'; 
				endif;
					echo'<td>'.$i.'</td>';
					echo'<td>'.$row['nama_kab'].'</td>';
					echo'<td><div class="action"><a class="update" href="'.site_url().'kabupaten/edit/'.$row['id_kab'].'/'.$row['id_provinsi'].'">Edit</a>';
					echo'<a class="delete" href="'.site_url().'kabupaten/delete/'.$row['id_kab'].'/'.$row['id_provinsi'].'">Hapus</a></div></td>';
				echo'</tr>';
			$i++;
			endforeach;
		echo '</table>';
	}
}

/* End of file agama.php */
/* Location: ./application/controllers/agama.php */